package view;

import Bean.Room;
import control.RoomManagerControl;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.sql.SQLException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JFrame;
import javax.swing.JOptionPane;

public class RoomCategoryPanel extends RootPanel
{

    RoomManagerControl rmControl = new RoomManagerControl();
    int index;
    int choice;
    Room Room = new Room();

    public RoomCategoryPanel(JFrame window)
    {
        initComponents();
        this.window = window;
        jTDanhmucphong.addMouseListener(new MouseAdapter()
        {
            @Override
            public void mousePressed(MouseEvent me)
            {
                index = jTDanhmucphong.getSelectedRow();
                choice = (int) jTDanhmucphong.getValueAt(index, 0);
                jTFChonphong.setText(Integer.toString(choice));
            }
        });
        setBgSearch(50, 95);
        setBgSearch(50, 500);
        setTime();
        setBackground("room_panel");

    }

    public void reset()
    {
        jTDanhmucphong.setModel(rmControl.getRoomCategory());
        jTDanhmucphong.getTableHeader().setReorderingAllowed(false);
        jCBLoaiphong.setSelected(false);
        jCBTen.setSelected(true);
        jCBTinhtrang.setSelected(false);
        jTFTimkiem.setVisible(true);
        jCBTinhTrang1.setVisible(false);
        jCBLoaiphong1.setVisible(false);
        jTFChonphong.setText(null);
        jTFTimkiem.setText(null);
        choice = 0;
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents()
    {

        jBTLKieuphong = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        jTDanhmucphong = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jBThemphong = new javax.swing.JButton();
        jBCapnhat = new javax.swing.JButton();
        jBXoaphong = new javax.swing.JButton();
        jLabel2 = new javax.swing.JLabel();
        jTFTimkiem = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        jCBTen = new javax.swing.JCheckBox();
        jCBTinhtrang = new javax.swing.JCheckBox();
        jCBLoaiphong = new javax.swing.JCheckBox();
        jCBTinhTrang1 = new javax.swing.JComboBox();
        jCBLoaiphong1 = new javax.swing.JComboBox();
        jTFChonphong = new javax.swing.JTextField();
        jLabel3 = new javax.swing.JLabel();

        jBTLKieuphong.setText("Thiết lập kiểu phòng");
        jBTLKieuphong.setFocusPainted(false);
        jBTLKieuphong.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBTLKieuphongActionPerformed(evt);
            }
        });

        jTDanhmucphong.setModel(rmControl.getRoomCategory());
        jScrollPane1.setViewportView(jTDanhmucphong);

        jLabel1.setFont(new java.awt.Font("Tahoma", 1, 14)); // NOI18N
        jLabel1.setForeground(new java.awt.Color(0, 51, 153));
        jLabel1.setText("DANH MỤC PHÒNG HIỆN CÓ");

        jBThemphong.setText("Thêm phòng");
        jBThemphong.setFocusPainted(false);
        jBThemphong.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBThemphongActionPerformed(evt);
            }
        });

        jBCapnhat.setText("Cập nhật phòng");
        jBCapnhat.setFocusPainted(false);
        jBCapnhat.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBCapnhatActionPerformed(evt);
            }
        });

        jBXoaphong.setText("Xóa phòng");
        jBXoaphong.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jBXoaphongActionPerformed(evt);
            }
        });

        jLabel2.setText("Tìm kiếm theo:");

        jButton1.setText("Tìm");
        jButton1.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jButton1ActionPerformed(evt);
            }
        });

        jCBTen.setText("Tên");
        jCBTen.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBTenActionPerformed(evt);
            }
        });

        jCBTinhtrang.setText("Tình trạng");
        jCBTinhtrang.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBTinhtrangActionPerformed(evt);
            }
        });

        jCBLoaiphong.setText("Loại phòng");
        jCBLoaiphong.addActionListener(new java.awt.event.ActionListener()
        {
            public void actionPerformed(java.awt.event.ActionEvent evt)
            {
                jCBLoaiphongActionPerformed(evt);
            }
        });

        jCBTinhTrang1.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Đã thuê", "Còn trống" }));

        jTFChonphong.setEditable(false);
        jTFChonphong.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N

        jLabel3.setFont(new java.awt.Font("Tahoma", 0, 12)); // NOI18N
        jLabel3.setText("Phòng được chọn");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(52, 52, 52)
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 553, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(24, 24, 24)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                            .addComponent(jBThemphong, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBTLKieuphong, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBCapnhat, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addComponent(jBXoaphong, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 135, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(329, 329, 329)
                        .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 212, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(61, 61, 61)
                        .addComponent(jLabel2)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCBTen)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCBTinhtrang)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBLoaiphong)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jTFTimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, 121, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jCBTinhTrang1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jCBLoaiphong1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(jButton1))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(70, 70, 70)
                        .addComponent(jLabel3)
                        .addGap(21, 21, 21)
                        .addComponent(jTFChonphong, javax.swing.GroupLayout.PREFERRED_SIZE, 195, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(62, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(31, 31, 31)
                .addComponent(jLabel1)
                .addGap(54, 54, 54)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2)
                    .addComponent(jCBTen)
                    .addComponent(jCBTinhtrang)
                    .addComponent(jCBLoaiphong)
                    .addComponent(jTFTimkiem, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton1)
                    .addComponent(jCBTinhTrang1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jCBLoaiphong1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(39, 39, 39)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jBTLKieuphong, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(20, 20, 20)
                        .addComponent(jBThemphong, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBCapnhat, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jBXoaphong, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 325, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jTFChonphong, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel3))
                .addContainerGap(172, Short.MAX_VALUE))
        );
    }// </editor-fold>//GEN-END:initComponents

    private void jBTLKieuphongActionPerformed(java.awt.event.ActionEvent evt)//GEN-FIRST:event_jBTLKieuphongActionPerformed
    {//GEN-HEADEREND:event_jBTLKieuphongActionPerformed
        RoomTypeDialog rtDialog = new RoomTypeDialog(this);
        rtDialog.setVisible(true);
        window.setEnabled(false);
        rtDialog.addWindowListener(onClose);
        reset();
    }//GEN-LAST:event_jBTLKieuphongActionPerformed

    private void jBThemphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBThemphongActionPerformed
        AddRoomDialog arDialog = new AddRoomDialog(this);
        arDialog.setVisible(true);
        window.setEnabled(false);
        arDialog.addWindowListener(onClose);
        reset();
    }//GEN-LAST:event_jBThemphongActionPerformed

    private void jBCapnhatActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBCapnhatActionPerformed

        if (choice == 0)
        {
            JOptionPane.showMessageDialog(null, "Chưa chọn phòng");
        }
        else
        {
            try
            {
                if (rmControl.TestInforCustomer(choice) == false)
                {
                    JOptionPane.showMessageDialog(null, "Không thể cập nhật");
                }
                else
                {
                    UpdateRoomDialog urDialog = new UpdateRoomDialog(this, choice, window);
                    urDialog.setVisible(true);
                    window.setEnabled(false);
                    urDialog.addWindowListener(onClose);
                }
            }
            catch (SQLException ex)
            {
                Logger.getLogger(RoomCategoryPanel.class.getName()).log(Level.SEVERE, null, ex);
            }
            reset();
        }
    }//GEN-LAST:event_jBCapnhatActionPerformed

    private void jBXoaphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jBXoaphongActionPerformed
        if (choice == 0)
        {
            JOptionPane.showMessageDialog(null, "Chưa chọn phòng");
        }
        else
        {
            try
            {
                if (rmControl.TestInforCustomer(choice) == false)
                {
                    JOptionPane.showMessageDialog(null, "Không thể xóa");
                }
                else
                {
                    if (JOptionPane.showConfirmDialog(this, "Bạn chắc chắn muốn xóa?",
                            "Xóa khách hàng", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION)
                    {
                        rmControl.deleteRoom(choice);
                        JOptionPane.showMessageDialog(null, "Xóa thành công");
                    }
                }


            }
            catch (SQLException ex)
            {
                Logger.getLogger(RoomCategoryPanel.class.getName()).log(Level.SEVERE, null, ex);
            }

        }
        reset();
    }//GEN-LAST:event_jBXoaphongActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        if (jCBTen.getSelectedObjects() != null)
        {
            String key = jTFTimkiem.getText();
            jTDanhmucphong.setModel(rmControl.filterNameRoom(key));
        }
        else
        {
            if (jCBLoaiphong.getSelectedObjects() != null)
            {
                String key = (String) jCBLoaiphong1.getSelectedItem();
                jTDanhmucphong.setModel(rmControl.filterTypeRoom(key));

            }
            else
            {
                String key = (String) jCBTinhTrang1.getSelectedItem();
                jTDanhmucphong.setModel(rmControl.filterStateRoom(key));

            }
        }

    }//GEN-LAST:event_jButton1ActionPerformed
    private void jCBTenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBTenActionPerformed
        jCBLoaiphong.setSelected(false);
        jCBTinhtrang.setSelected(false);
        jTFTimkiem.setVisible(true);
        jCBTinhTrang1.setVisible(false);
        jCBLoaiphong1.setVisible(false);
    }//GEN-LAST:event_jCBTenActionPerformed

    private void jCBTinhtrangActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBTinhtrangActionPerformed
        jTFTimkiem.setText(null);
        jCBLoaiphong.setSelected(false);
        jCBTen.setSelected(false);
        jTFTimkiem.setVisible(false);
        jCBTinhTrang1.setVisible(true);
        jCBLoaiphong1.setVisible(false);

    }//GEN-LAST:event_jCBTinhtrangActionPerformed

    private void jCBLoaiphongActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCBLoaiphongActionPerformed
        jTFTimkiem.setText(null);
        jTFChonphong.setText(null);
        jTFTimkiem.setText(null);
        jCBTen.setSelected(false);
        jCBTinhtrang.setSelected(false);
        jTFTimkiem.setVisible(false);
        jCBTinhTrang1.setVisible(false);
        jCBLoaiphong1.setVisible(true);
        jCBLoaiphong1.removeAllItems();
        List<Room> list = rmControl.getRoomTypeName();
        for (Room obj_room : list)
        {
            jCBLoaiphong1.addItem(obj_room.getTyperoom().toString());
        }

    }//GEN-LAST:event_jCBLoaiphongActionPerformed
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jBCapnhat;
    private javax.swing.JButton jBTLKieuphong;
    private javax.swing.JButton jBThemphong;
    private javax.swing.JButton jBXoaphong;
    private javax.swing.JButton jButton1;
    private javax.swing.JCheckBox jCBLoaiphong;
    private javax.swing.JComboBox jCBLoaiphong1;
    private javax.swing.JCheckBox jCBTen;
    private javax.swing.JComboBox jCBTinhTrang1;
    private javax.swing.JCheckBox jCBTinhtrang;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable jTDanhmucphong;
    private javax.swing.JTextField jTFChonphong;
    private javax.swing.JTextField jTFTimkiem;
    // End of variables declaration//GEN-END:variables

    public void Show()
    {
        super.Show();
        reset();
    }
}
